package com.education.platform.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.education.platform.common.Result;
import com.education.platform.dto.demand.DemandCreateDTO;
import com.education.platform.entity.Demand;
import com.education.platform.service.DemandService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 需求控制器
 *
 * @author education-platform
 * @since 2024-09-27
 */
@Tag(name = "需求管理", description = "家长需求发布和管理相关接口")
@RestController
@RequestMapping("/api/demand")
@RequiredArgsConstructor
@Slf4j
public class DemandController {

    private final DemandService demandService;

    @Operation(summary = "发布需求", description = "家长发布教学需求")
    @PostMapping("/publish")
    @SaCheckLogin
    public Result<Long> publishDemand(@Validated @RequestBody DemandCreateDTO demandCreateDTO) {
        Long userId = StpUtil.getLoginIdAsLong();
        Long demandId = demandService.createDemand(demandCreateDTO, userId);
        return Result.success(demandId);
    }

    @Operation(summary = "测试发布需求", description = "测试发布需求接口（无需登录）")
    @PostMapping("/test-publish")
    public Result<Long> testPublishDemand(@Validated @RequestBody DemandCreateDTO demandCreateDTO) {
        Long testUserId = 1L;
        Long demandId = demandService.createDemand(demandCreateDTO, testUserId);
        return Result.success(demandId);
    }

    @Operation(summary = "更新需求", description = "更新需求信息")
    @PutMapping("/{id}")
    @SaCheckLogin
    public Result<Void> updateDemand(@PathVariable Long id,
                                    @Validated @RequestBody DemandCreateDTO demandCreateDTO) {
        Long userId = StpUtil.getLoginIdAsLong();
        demandService.updateDemand(id, demandCreateDTO, userId);
        return Result.success();
    }

    @Operation(summary = "删除需求", description = "删除需求")
    @DeleteMapping("/{id}")
    @SaCheckLogin
    public Result<Void> deleteDemand(@PathVariable Long id) {
        Long userId = StpUtil.getLoginIdAsLong();
        demandService.deleteDemand(id, userId);
        return Result.success();
    }

    @Operation(summary = "获取需求详情", description = "根据ID获取需求详细信息")
    @GetMapping("/{id}")
    public Result<Demand> getDemandDetail(@PathVariable Long id) {
        // 增加浏览次数
        demandService.incrementViewCount(id);
        Demand demand = demandService.getDemandById(id);
        return Result.success(demand);
    }

    @Operation(summary = "分页查询需求列表", description = "分页查询需求列表，支持筛选")
    @GetMapping("/page")
    public Result<IPage<Demand>> getDemandPage(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer current,
            @Parameter(description = "页大小") @RequestParam(defaultValue = "10") Integer size,
            @Parameter(description = "科目") @RequestParam(required = false) String subject,
            @Parameter(description = "年级") @RequestParam(required = false) String grade,
            @Parameter(description = "授课方式") @RequestParam(required = false) String teachingType,
            @Parameter(description = "状态") @RequestParam(required = false) String status) {

        Page<Demand> page = new Page<>(current, size);
        IPage<Demand> result = demandService.getDemandPage(page, subject, grade, teachingType, status);
        return Result.success(result);
    }

    @Operation(summary = "获取我的需求列表", description = "获取当前用户发布的需求列表")
    @GetMapping("/my")
    @SaCheckLogin
    public Result<List<Demand>> getMyDemands() {
        Long userId = StpUtil.getLoginIdAsLong();
        List<Demand> demands = demandService.getDemandsByParentId(userId);
        return Result.success(demands);
    }

    @Operation(summary = "关闭需求", description = "手动关闭需求")
    @PostMapping("/{id}/close")
    @SaCheckLogin
    public Result<Void> closeDemand(@PathVariable Long id) {
        Long userId = StpUtil.getLoginIdAsLong();
        demandService.closeDemand(id, userId);
        return Result.success();
    }

    @Operation(summary = "获取热门需求", description = "获取热门需求列表")
    @GetMapping("/hot")
    public Result<List<Demand>> getHotDemands(
            @Parameter(description = "数量限制") @RequestParam(defaultValue = "10") Integer limit) {
        List<Demand> demands = demandService.getHotDemands(limit);
        return Result.success(demands);
    }

    @Operation(summary = "获取即将过期的需求", description = "获取即将过期的需求列表")
    @GetMapping("/expiring")
    @SaCheckLogin
    public Result<List<Demand>> getExpiringDemands() {
        List<Demand> demands = demandService.getExpiringDemands();
        return Result.success(demands);
    }
}
